import 'package:local_coffee/components/hex.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:flutter/material.dart';
import 'package:local_coffee/components/c_button/index.dart';
import 'package:local_coffee/jsonserialize/goods_detail/data.dart';

class SelectRow extends StatelessWidget {

  final GoodsDetailProperty data;
  final Function onChange;
  final id;

  SelectRow({
    @required this.data,
    @required this.onChange,
    @required this.id,
  });

  Container _createRadio({
    GoodsDetailProperty item
  }) {
    bool isActive = id == item.id;

    return Container(
      key: Key('${item.id}'),
      margin: EdgeInsets.only(right: 10),
      child: CButton.normal(
        width: 80,
        height: 30,
        borderRadius: BorderRadius.circular(15),
        child: Text(
          '${item.name}',
          style: TextStyle(
            fontSize: 12
          ),
        ),
        plain: !isActive,
        color: isActive ? HEX.hexfff : RGBA.rgbaccc0b4,
        bgColor: isActive ? RGBA.rgbaccc0b4 : HEX.hexfff,
        borderColor: isActive ? Colors.transparent : RGBA.rgbaccc0b4,
        onPressed: () {
          Map<String, dynamic> type = {
            'typeId': data.id,
            'childId': item.id,
            'typeName': data.name,
            'childName': item.name
          };

          onChange(type);
        }
      ),
    );
  }

  /// 初始化选项
  List<Widget> _initOptions() {
    List<Widget> widgets = [];
    data.childsCurGoods.forEach((GoodsDetailProperty item) {
      widgets.add(_createRadio(item: item));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 6, bottom: 6),
      child: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(bottom: 10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 65,
                  alignment: Alignment.center,
                  height: 30,
                  child: Text(
                    '${data.name}',
                    style: TextStyle(
                      color: RGBA.rgba383838
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Wrap(
                      children: _initOptions(),
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}